package com.examine.serviceImpl;

import com.examine.bean.Message;
import com.examine.bean.News;
import com.examine.dao.MessageMapper;
import com.examine.dao.NewsMapper;
import com.examine.service.MessageService;
import com.examine.utils.DataConfig;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class MessageServiceImpl implements MessageService {
    private static SqlSession session = null;
    private static MessageMapper messageMapper = null;
    private Message message = null;
    private List<Message> messageList = null;
    private int count = 0;

    public static void dataConfig(){
        try{
            session = DataConfig.getSession();
            messageMapper = session.getMapper(MessageMapper.class);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        dataConfig();

        try{
            count = messageMapper.deleteByPrimaryKey(id);
            if (count > 0){
                session.commit();
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return count;
    }

    @Override
    public int insertSelective(Message record) {
        dataConfig();

        try{
            count = messageMapper.insertSelective(record);
            if (count > 0){
                session.commit();
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return count;
    }

    @Override
    public Message selectByPrimaryKey(String id) {
        dataConfig();

        try{
            message = messageMapper.selectByPrimaryKey(id);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return message;
    }

    @Override
    public int updateByPrimaryKeySelective(Message record) {
        dataConfig();

        try{
            count = messageMapper.updateByPrimaryKeySelective(record);
            if (count > 0){
                session.commit();
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return count;
    }

    @Override
    public List<Message> getAllMessageList(Message record) {
        dataConfig();
        try{
            messageList = messageMapper.getAllMessageList(record);
            for(Message message1 : messageList){

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return messageList;
    }

    @Override
    public int queryCountLike(Message record) {
        dataConfig();

        try{
            count = messageMapper.queryCountLike(record);
            if (count > 0){
                session.commit();
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return count;
    }

    @Override
    public int newsArticleNumber(Message record) {
        dataConfig();

        try{
            count = messageMapper.newsArticleNumber(record);
            if (count > 0){
                session.commit();
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return count;
    }
}
